﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.StopZero.CApStopZeroUtilities
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;


namespace DnProcessSimulateCommands.StopZero;

public static class CApStopZeroUtilities
{
  public static ITxObject GetActiveGun(ITxRoboticLocationOperation location)
  {
    if (location == null)
      return (ITxObject) null;
    ITxRobot assignedRobot = CApStopZeroUtilities.GetAssignedRobot(location);
    if (assignedRobot == null)
      return (ITxObject) null;
    ITxRoboticControllerServices controllerServices = CApStopZeroUtilities.GetControllerServices(assignedRobot);
    return CApStopZeroUtilities.GetActiveGun(location, assignedRobot, controllerServices);
  }

  public static ITxRobot GetAssignedRobot(ITxRoboticLocationOperation location)
  {
    return location == null || location.ParentRoboticOperation == null ? (ITxRobot) null : location.ParentRoboticOperation.Robot;
  }

  public static bool IsStopZeroCapable(ITxRobot robot)
  {
    try
    {
      TxRobot txRobot = robot as TxRobot;
      TxRRSServicesProvider servicesProvider = new TxRRSServicesProvider(txRobot);
      return TxRRSServicesProvider.DoesRcsExist(txRobot) && TxRRSServicesProviderEx.DoesSupportStopSimulation(servicesProvider, 0);
    }
    catch (TxRRSCanNotInitializeRcsException ex)
    {
      return false;
    }
  }

  public static bool AreInSameOperation(
    ITxRoboticLocationOperation first,
    ITxRoboticLocationOperation second)
  {
    return first != null && second != null && first.ParentRoboticOperation == second.ParentRoboticOperation;
  }

  public static bool AreInSameOrder(
    ITxRoboticLocationOperation first,
    ITxRoboticLocationOperation second)
  {
    if (first == null || second == null || !CApStopZeroUtilities.AreInSameOperation(first, second))
      return false;
    ITxRoboticOrderedCompoundOperation roboticOperation = first.ParentRoboticOperation;
    return ((ITxOrderedObjectCollection) roboticOperation).GetIndexOfChild((ITxObject) first) < ((ITxOrderedObjectCollection) roboticOperation).GetIndexOfChild((ITxObject) second);
  }

  public static ITxRoboticControllerServices GetControllerServices(ITxRobot robot)
  {
    if (robot == null)
      return (ITxRoboticControllerServices) null;
    TxOlpControllerUtilities controllerUtilities = new TxOlpControllerUtilities();
    if (!(controllerUtilities.GetInterfaceImplementationFromController(robot.Controller.Name, typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName") is ITxRoboticControllerServices implementationFromController))
      implementationFromController = controllerUtilities.GetInterfaceImplementationFromController("default", typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName") as ITxRoboticControllerServices;
    return implementationFromController;
  }

  private static ITxObject GetActiveGun(
    ITxRoboticLocationOperation location,
    ITxRobot robot,
    ITxRoboticControllerServices controllerServices)
  {
    if (location == null || robot == null || controllerServices == null)
      return (ITxObject) null;
    ITxObject activeGun = (ITxObject) null;
    try
    {
      controllerServices.Init(robot as TxRobot);
      activeGun = (ITxObject) controllerServices.GetActiveGun((ITxLocationOperation) location) ?? (ITxObject) controllerServices.GetActiveGripper((ITxLocationOperation) location);
      if (activeGun != null)
      {
        if (!activeGun.IsValid())
          activeGun = (ITxObject) null;
      }
    }
    catch
    {
    }
    return activeGun;
  }

  public static List<ITxObject> GetActiveTools(ITxRoboticLocationOperation location)
  {
    List<ITxObject> activeTools = new List<ITxObject>();
    ITxObject activeGun = CApStopZeroUtilities.GetActiveGun(location);
    if (activeGun == null)
    {
      ITxRobot assignedRobot = CApStopZeroUtilities.GetAssignedRobot(location);
      if (assignedRobot != null && ((Collection<ITxObject>) assignedRobot.MountedTools).Count > 0)
        activeTools.AddRange((IEnumerable<ITxObject>) assignedRobot.MountedTools);
    }
    else
      activeTools.Add(activeGun);
    return activeTools;
  }

  public static List<ITxObject> GetMountedTools(ITxRoboticLocationOperation location)
  {
    List<ITxObject> mountedTools = new List<ITxObject>();
    ITxRobot assignedRobot = CApStopZeroUtilities.GetAssignedRobot(location);
    if (assignedRobot != null && ((Collection<ITxObject>) assignedRobot.MountedTools).Count > 0)
      mountedTools.AddRange((IEnumerable<ITxObject>) assignedRobot.MountedTools);
    return mountedTools;
  }
}
